import axios from 'axios'
import { Message } from 'element-ui'
import { setLocalStorage,getLocalStorage } from '@/utils/utils'


const instance = axios.create({
  baseURL:"http://localhost:3000",
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json'
  }
})
// http request 请求拦截器
instance.interceptors.request.use(
  config => {
    config.headers.AcceptLanguage = getLocalStorage("locale");
    if (localStorage.jwtToken) {
      config.headers.Authorization = getLocalStorage("jwtToken");
    }
    console.log('config',config);
    return config
  },
  err => {
    return Promise.reject(err)
  }
)
// http response 响应拦截器
instance.interceptors.response.use(
  response => {
    console.log('response',response);
    return handleData(response.data)
  },
  error => {
    if(error.response.data.status===500&&(error.response.data.message==='token out time'||error.response.data.message==='登录失败或未登录')){
      setLocalStorage('jwtToken');
      window.location.href = baseUrl.url1;
    }
    let err = error.response.data.message;
    if (err != '' && err != null && err != undefined) {
      Message({
        type: 'error',
        message: error.response.data.message
      })
      return Promise.reject(error.response.data)
    } else {
      Message({
        type: 'error',
        message: 'HTTP:服务器遇到错误,无法完成请求。'
      })
    }
  })

